package com.M.book.controller;

import com.M.book.constant.Constants;
import com.M.book.model.*;
import com.M.book.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
//    private BookService bookService = new BookService();
    @Autowired
    private BookService bookService;

//    @RequestMapping("/getBookList")
//    public List<BookInfo> getBookList() {
//        return bookService.getBookList();
//    }
    @RequestMapping("/getBookListByPage")
//    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest) {
    public Result getBookListByPage(PageRequest pageRequest, HttpSession session) {
        log.info("查询翻页信息, pageRequest = {}", pageRequest);
//        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
//        if(userInfo == null || userInfo.getId() <= 0 || "".equals(userInfo.getUserName())) {
//            return Result.unlogin();
//        }

        if(pageRequest.getPageSize() < 0 || pageRequest.getCurrentPage() < 1)
        {
//            return null;
            return Result.fail("参数晓燕失败");
        }


        PageResult<BookInfo> bookInfoPageResult = null;
        try{
            bookInfoPageResult = bookService.selectBookInfoByPage(pageRequest);
            return Result.success(bookInfoPageResult);
        }catch (Exception e) {
//            log.info(pageRequest.toString());
            log.error("查询翻页信息错误, e = {}", e.getMessage());
            return Result.fail(e.getMessage());
        }
//        return bookInfoPageResult;
    }

    /**
     * 添加图书
     * @param bookInfo
     * @return
     */
//    @RequestMapping("/addBook")
    @RequestMapping(value = "/addBook", produces = "application/json")
    public String addBook(BookInfo bookInfo) {
        log.info("接收到添加图书请求, bookInfo:" + bookInfo);

        if(!StringUtils.hasLength(bookInfo.getBookName())
        || !StringUtils.hasLength(bookInfo.getAuthor())
        || bookInfo.getCount() < 0
        || bookInfo.getPrice() == null
        || !StringUtils.hasLength(bookInfo.getPublish())){
            return "参数校验失败, 请检查入参";
        }

        Integer result = bookService.addBook(bookInfo);
        if(result <= 0){
            log.error("添加图书失败: bookInfo:" + bookInfo);
            return "添加图书失败, 请联系管理人员";
        }

        return "";
    }

    @RequestMapping("/queryBookInfoById")
    public BookInfo queryBookInfoById(Integer bookId) {
        log.info("根据ID查询图书, bookId" + bookId);

        BookInfo bookInfo = null;
        try{
//            return bookService.queryBookInfoById(bookId);
            bookInfo = bookService.queryBookInfoById(bookId);
        }catch (Exception e) {
            log.error("查询图示失败, e" + e.getMessage());
        }

//        return null;
        return bookInfo;
    }

//    @RequestMapping("/updateBook")
    @RequestMapping(value = "/updateBook", produces = "application/json")
    public String updateBook(BookInfo bookInfo) {
        log.info("接收到更新图书的请求, bookInfo:" + bookInfo);
        Integer result = bookService.updateBook(bookInfo);
        if(result == 0){
            log.error("更新图书失败, 请联系管理员");
            return "更新图书失败, 请联系管理员";
        }

        return "";
    }

//    @RequestMapping("/batchDelete")
    @RequestMapping(value = "/batchDelete", produces = "application/json")
    public String batchDelete(@RequestParam List<Integer> ids) {
        log.info("接收请求, 批量删除图书ids:" + ids);
        Integer result = bookService.batchDelete(ids);
        if(result <= 0){
            log.error("批量删除失败, ids:" + ids);
            return "批量删除失败";
        }
        return "";
    }
}
